package com.softmaker.controller;

import com.softmaker.entity.Video;
import com.softmaker.service.VideoService;
import com.softmaker.utils.ResultVOUtil;
import com.softmaker.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/video")
public class VideoController {
    @Value("${filepath.linux}")
    private String pathd;
    @Autowired
    private VideoService service;

    @PostMapping("/addVideo")
    public Result<String> addVideo(@RequestParam("file") MultipartFile file) throws IOException {
        Video video = new Video();
        String fileName = file.getOriginalFilename();
        if (service.findVideo(fileName) != null) {
            return ResultVOUtil.error("视频已存在");
        }
        video.setVideoName(fileName);
        // 设置临时文件路径
        // Windows路径
        String tmpFilePath = pathd;
        // Linux路径
//        String tmpFilePath ="/home/server/video/";
        File temp = new File(tmpFilePath);
        if (!temp.exists()) {
            temp.mkdirs();
        }
        // 获取文件后缀名
        Integer endIndexOf = fileName.lastIndexOf(".");
        // 上传文件后的后缀名
        String endFile = fileName.substring(endIndexOf, fileName.length());
        // 新名字
        String newName = UUID.randomUUID().toString() + endFile;
        String path = tmpFilePath + newName;
        video.setVideoAddress(path);
        video.setVideoId(UUID.randomUUID().toString());
        // 转移到临时文件路径
        File localFile = new File(path);
        file.transferTo(localFile);
        // 添加视频到数据库
        service.addVideo(video);
        return ResultVOUtil.success("成功");
    }

    @GetMapping("/getVideo")
    public Result<String> getVideo(@RequestParam("videoName") String videoName, HttpServletResponse response) throws IOException {
        // 根据视频名称获取视频路径
        String path = service.findVideo(videoName).getVideoAddress();
        // 创建文件对象
        File files = new File(path);
        // 创建字节数组
        byte[] bytes = new byte[1024];
        try (OutputStream os = response.getOutputStream();
             FileInputStream fis = new FileInputStream(files)) {
            // 读取文件内容并写入输出流
            while ((fis.read(bytes)) != -1) {
                os.write(bytes);
                os.flush();
            }
        }
        // 返回成功结果
        return ResultVOUtil.success("成功");
    }

    @GetMapping("/listVideo")
    public Result<List<Video>> listVideo() {
        List<Video> list = service.listVideo();
        return ResultVOUtil.success(list);
    }

    @PostMapping("/delVideo")
    public Result<String> delVideo(@RequestBody Video video) {
        service.deleteVideo(video);
        return ResultVOUtil.success("成功");
    }

    @PostMapping("/androidGetVideo")
    public Result<String> androidGetVideo(@RequestParam("videoName") String videoName) throws IOException {
        String path = service.findVideo(videoName).getVideoAddress();
        // 创建文件对象
        File file = new File(path);


        // 2. 创建 FileInputStream 对象以读取文件
        FileInputStream fis = new FileInputStream(file);

        // 3. 创建一个字节数组，用于存储文件内容
        byte[] byteArray = new byte[(int) file.length()];

        // 4. 读取文件内容到字节数组
        fis.read(byteArray);

        // 5. 关闭 FileInputStream
        fis.close();

        // 6. 将字节数组转换为二进制字符串
        String binaryString = service.bytesToBinaryString(byteArray);

        // 7. 打印二进制字符串
        System.out.println("Binary representation of the file content:\n" + binaryString);


        return ResultVOUtil.success(binaryString);



    }
    @GetMapping("/findVideoInfo")
    public Result<Video> findVideoInfo(@RequestParam("videoName") String videoName) {
        Video video = service.findVideo(videoName);
        return ResultVOUtil.success(video);
    }
    @PostMapping("/addVideoImage")
    public Result<String> addVideoImage(@RequestParam("file") MultipartFile file,@RequestParam("videoName") String videoName) throws IOException {
        Video video = service.findVideo(videoName);
        String fileName = file.getOriginalFilename();
        if (service.findVideoImage(fileName) != null) {
            return ResultVOUtil.error("图片已存在");
        }
        video.setVideoImageName(fileName);
        // 设置临时文件路径
        // Windows路径
        String tmpFilePath = pathd;
        // Linux路径
//        String tmpFilePath ="/home/server/video/";
        File temp = new File(tmpFilePath);
        if (!temp.exists()) {
            temp.mkdirs();
        }
        // 获取文件后缀名
        Integer endIndexOf = fileName.lastIndexOf(".");
        // 上传文件后的后缀名
        String endFile = fileName.substring(endIndexOf, fileName.length());
        // 新名字
        String newName = UUID.randomUUID().toString() + endFile;
        String path = tmpFilePath + newName;
        video.setVideoImageAddress(path);
        // 转移到临时文件路径
        File localFile = new File(path);
        file.transferTo(localFile);
        // 添加视频到数据库
        service.addVideo(video);
        return ResultVOUtil.success("成功");
    }
    @GetMapping("/getVideoImage")
    public Result<String> getVideoImage(@RequestParam("videoImageName") String videoImageName, HttpServletResponse response) throws IOException {
        // 根据视频名称获取视频路径
        String path = service.findVideoImage(videoImageName).getVideoImageAddress();
        // 创建文件对象
        File files = new File(path);
        // 创建字节数组
        byte[] bytes = new byte[1024];
        try (OutputStream os = response.getOutputStream();
             FileInputStream fis = new FileInputStream(files)) {
            // 读取文件内容并写入输出流
            while ((fis.read(bytes)) != -1) {
                os.write(bytes);
                os.flush();
            }
        }
        // 返回成功结果
        return ResultVOUtil.success("成功");
    }
}